import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {FormControl, Validators, FormGroup, FormBuilder, FormArray} from '@angular/forms';
 

@Component({
  templateUrl: './dynamic-form.component.html',
  changeDetection:ChangeDetectionStrategy.OnPush
})
export class DynamicFormComponent implements OnInit{
  constructor(private fb: FormBuilder){}
  ctrls;
  formGroup: FormGroup;

  ngOnInit() {
    this.initForm();
  }

  initForm(){
    this.formGroup = this.fb.group({
      luns: this.fb.array([new FormControl('lun', Validators.required)])
    });
    this.ctrls = (this.formGroup.get('luns') as FormArray).controls;
  }

  add(){
    (this.formGroup.get('luns') as FormArray).push(new FormControl('new lun', Validators.required));
  }

  remove(i){
    (this.formGroup.get('luns') as FormArray).removeAt(i);
  }
}